<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\AuthController;
use App\Http\Controllers\Auth\VerificationController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\MoneyBookController;
use App\Http\Controllers\UserBookController;
use App\Http\Controllers\MoneyCategoryController;
use App\Http\Controllers\MoneyTransactionController;
use App\Http\Controllers\AutoTransactionController;
use App\Http\Controllers\DetailController;
use App\Http\Controllers\StatisticsController;
use App\Http\Controllers\MembershipController;
use App\Http\Controllers\WechatController;
use App\Http\Controllers\ArticleController;

// 访客路由（未登录用户）
Route::middleware('guest')->group(function () {
    Route::get('/register', [AuthController::class, 'showRegisterForm'])->name('register');
    Route::post('/register', [AuthController::class, 'register']);
    Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [AuthController::class, 'login']);
});

// 微信浏览器检测路由（不需要认证）
Route::get('/wechat/check-browser', [WechatController::class, 'checkWechatBrowser'])->name('wechat.check-browser');
Route::get('/wechat/login', [WechatController::class, 'login'])->name('wechat.login');
Route::get('/wechat/login/callback', [WechatController::class, 'loginCallback'])->name('wechat.login.callback');

// 微信绑定相关路由（不需要认证）
Route::get('/wechat/bind', [WechatController::class, 'showBindPage'])->name('wechat.bind');
Route::get('/wechat/login-bind', [WechatController::class, 'showLoginBindPage'])->name('wechat.login.bind');
Route::get('/wechat/register-bind', [WechatController::class, 'showRegisterBindPage'])->name('wechat.register.bind');
Route::post('/wechat/login-bind', [WechatController::class, 'submitLoginBind'])->name('wechat.login.bind.submit');
Route::post('/wechat/register-bind', [WechatController::class, 'submitRegisterBind'])->name('wechat.register.bind.submit');

// 关于我们和协议相关路由（不需要认证）
Route::get('/about-us', [ArticleController::class, 'aboutUs'])->name('about.us');
Route::get('/article/{key}', [ArticleController::class, 'article'])->name('article.show');

// 免登录下载路由
Route::get('/download/{token}', [App\Http\Controllers\BillExportController::class, 'publicDownload'])->name('public.download');

// 需要认证的路由
// 邀请相关路由（无需认证）
Route::get('/invite/{token}', [MoneyBookController::class, 'showInvite'])->name('invite.show');
Route::post('/invite/{token}/accept', [MoneyBookController::class, 'acceptInvite'])->name('invite.accept')->middleware('auth');

// 添加微信浏览器检测中间件
Route::middleware(['wechat.browser', 'auth'])->group(function () {
    // 首页
    Route::get('/', [HomeController::class, 'index'])->name('home');
    Route::get('/home', [HomeController::class, 'index'])->name('home.index');
    Route::get('/home/stats', [HomeController::class, 'getStats'])->name('home.stats');

    // 仪表板


    // 登出
    Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

    // 用户中心相关路由
    Route::get('/user/profile', [UserController::class, 'profile'])->name('user.profile');
    Route::post('/user/profile', [UserController::class, 'updateProfile'])->name('user.profile.update');
    Route::post('/user/change-password', [UserController::class, 'changePassword'])->name('user.change-password');
    Route::get('/user/info', [UserController::class, 'getUserInfo'])->name('user.info');

    // 用户账本管理路由
    Route::get('/user/books', [UserBookController::class, 'index'])->name('user.books');
    Route::post('/user/books', [MoneyBookController::class, 'store'])->name('user.books.store');
    Route::put('/user/books/{id}', [UserBookController::class, 'update'])->name('user.books.update');
    Route::post('/user/books/{id}/set-default', [UserBookController::class, 'setDefault'])->name('user.books.set-default');
    Route::delete('/user/books/{id}', [UserBookController::class, 'destroy'])->name('user.books.destroy');
    Route::post('/user/books/{id}/leave', [UserBookController::class, 'leave'])->name('user.books.leave');

    // 账本相关路由
    Route::get('/user/books/data', [MoneyBookController::class, 'getBooks'])->name('user.books.data');
    Route::post('/user/books/{id}/invite', [MoneyBookController::class, 'generateInviteLink'])->name('user.books.invite');
    Route::get('/user/books/{id}/members', [MoneyBookController::class, 'getMembers'])->name('user.books.members');
    Route::delete('/user/books/{bookId}/members/{userId}', [MoneyBookController::class, 'removeMember'])->name('user.books.remove-member');

    // 分类管理页面
    Route::get('/user/books/{book}/categories', [MoneyBookController::class, 'categoryManagement'])->name('user.books.categories');
    Route::get('/user/books/{bookId}/categories/data', [MoneyCategoryController::class, 'getCategories'])->name('user.categories.data');
    Route::get('/user/books/{bookId}/categories/data/{type}', [MoneyCategoryController::class, 'getCategories'])->name('user.categories.data.type');
    Route::post('/user/books/{bookId}/categories', [MoneyCategoryController::class, 'store'])->name('user.categories.store');
    Route::put('/user/books/{bookId}/categories/{id}', [MoneyCategoryController::class, 'update'])->name('user.categories.update');
    Route::delete('/user/books/{bookId}/categories/{id}', [MoneyCategoryController::class, 'destroy'])->name('user.categories.destroy');
    Route::post('/user/books/{bookId}/categories/{id}/migrate', [MoneyCategoryController::class, 'migrateAndDestroy'])->name('user.categories.migrate');
    Route::post('/user/books/{bookId}/categories/sort', [MoneyCategoryController::class, 'updateSort'])->name('user.categories.sort');

    // 子分类管理路由
    Route::get('/user/books/{bookId}/categories/{categoryId}/subcategories', [MoneyCategoryController::class, 'subcategoryManagement'])->name('user.categories.subcategories');
    Route::get('/user/books/{bookId}/categories/{categoryId}/subcategories/data', [MoneyCategoryController::class, 'getSubCategories'])->name('user.subcategories.data');
    Route::post('/user/books/{bookId}/categories/{categoryId}/subcategories', [MoneyCategoryController::class, 'storeSubCategory'])->name('user.subcategories.store');
    Route::put('/user/books/{bookId}/categories/{categoryId}/subcategories/{id}', [MoneyCategoryController::class, 'updateSubCategory'])->name('user.subcategories.update');
    Route::delete('/user/books/{bookId}/categories/{categoryId}/subcategories/{id}', [MoneyCategoryController::class, 'destroySubCategory'])->name('user.subcategories.destroy');
    Route::post('/user/books/{bookId}/categories/{categoryId}/subcategories/sort', [MoneyCategoryController::class, 'updateSubCategorySort'])->name('user.subcategories.sort');
    Route::post('/user/books/{bookId}/categories/{categoryId}/subcategories/{id}/migrate', [MoneyCategoryController::class, 'migrateAndDestroySubCategory'])->name('user.subcategories.migrate');

    // Transaction routes
    Route::get('/transaction/create', [MoneyTransactionController::class, 'create'])->name('transaction.create');
    Route::post('/transaction', [MoneyTransactionController::class, 'store'])->name('transaction.store');
    Route::post('/transaction/ai', [MoneyTransactionController::class, 'storeByAi'])->name('transaction.storeByAi');
    Route::get('/transaction/{id}', [MoneyTransactionController::class, 'show'])->name('transaction.show');
    Route::get('/transaction/{id}/edit', [MoneyTransactionController::class, 'edit'])->name('transaction.edit');
    Route::put('/transaction/{id}', [MoneyTransactionController::class, 'update'])->name('transaction.update');
    Route::delete('/transaction/{id}', [MoneyTransactionController::class, 'destroy'])->name('transaction.destroy');

    // 定时记账路由
    Route::get('/auto-transaction', [AutoTransactionController::class, 'index'])->name('auto-transaction.index');
    Route::get('/auto-transaction/data', [AutoTransactionController::class, 'getData'])->name('auto-transaction.data');
    Route::get('/auto-transaction/create', [AutoTransactionController::class, 'create'])->name('auto-transaction.create');
    Route::post('/auto-transaction', [AutoTransactionController::class, 'store'])->name('auto-transaction.store');
    Route::get('/auto-transaction/{id}/edit', [AutoTransactionController::class, 'edit'])->name('auto-transaction.edit');
    Route::put('/auto-transaction/{id}', [AutoTransactionController::class, 'update'])->name('auto-transaction.update');
    Route::patch('/auto-transaction/{id}/toggle-status', [AutoTransactionController::class, 'toggleStatus'])->name('auto-transaction.toggle-status');
    Route::delete('/auto-transaction/{id}', [AutoTransactionController::class, 'destroy'])->name('auto-transaction.destroy');

    // 明细路由
    Route::get('/details', [DetailController::class, 'index'])->name('details.index');
    Route::get('/details/data', [DetailController::class, 'getData'])->name('details.data');
    Route::get('/details/filter-options', [DetailController::class, 'getFilterOptions'])->name('details.filter-options');

    // 统计分析路由
    Route::get('/statistics', [StatisticsController::class, 'index'])->name('statistics.index');
    Route::get('/statistics/trend', [StatisticsController::class, 'getTrendData'])->name('statistics.trend');
    Route::get('/statistics/category', [StatisticsController::class, 'getCategoryData'])->name('statistics.category');
    Route::get('/statistics/comparison', [StatisticsController::class, 'getMonthlyComparison'])->name('statistics.comparison');
    Route::get('/statistics/category-detail', [StatisticsController::class, 'categoryDetail'])->name('statistics.category-detail');
    Route::get('/statistics/category-detail-data', [StatisticsController::class, 'getCategoryDetailData'])->name('statistics.category-detail-data');

    // 会员相关路由
    Route::prefix('membership')->name('membership.')->group(function () {
        Route::get('/', [MembershipController::class, 'index'])->name('index');
        Route::get('/benefits', [MembershipController::class, 'benefits'])->name('benefits');
        Route::get('/redeem', [MembershipController::class, 'redeemForm'])->name('redeem.form');
        Route::post('/redeem', [MembershipController::class, 'redeem'])->name('redeem');
        Route::get('/status', [MembershipController::class, 'status'])->name('status');
        Route::get('/history', [MembershipController::class, 'history'])->name('history');
        Route::get('/history/data', [MembershipController::class, 'getHistoryData'])->name('history.data');
    });

    // 账单导出
    Route::prefix('bill-export')->name('bill-export.')->group(function () {
        Route::get('/', [App\Http\Controllers\BillExportController::class, 'index'])->name('index');
        Route::get('/history', [App\Http\Controllers\BillExportController::class, 'history'])->name('history');
        Route::post('/', [App\Http\Controllers\BillExportController::class, 'store'])->name('store');
        Route::get('/download/{id}', [App\Http\Controllers\BillExportController::class, 'download'])->name('download');
        Route::get('/{id}/public-url', [App\Http\Controllers\BillExportController::class, 'getPublicUrl'])->name('public-url');
        Route::get('/status/{id}', [App\Http\Controllers\BillExportController::class, 'status'])->name('status');
    });
});

// 微信消息处理路由（不需要认证）
Route::any('/wechat/message', [WechatController::class, 'handleMessage'])->name('wechat.message')->withoutMiddleware(['web']);

// 微信相关路由
Route::middleware('auth')->group(function () {
    Route::get('/wechat/bind-account', [WechatController::class, 'bind'])->name('wechat.bind-account');
    Route::get('/wechat/callback', [WechatController::class, 'callback'])->name('wechat.callback');
    Route::post('/wechat/unbind', [WechatController::class, 'unbind'])->name('wechat.unbind');

    // 微信菜单管理（仅管理员可访问）
    Route::middleware('admin')->group(function () {
        Route::get('/wechat/create-menu', [WechatController::class, 'createMenu'])->name('wechat.create-menu');
    });
});
